[20220812_153854_user] D2_main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. #include "stdio.h"
  28. /* Private variables ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. #define VECT_TAB_OFFSET 0x10000
  31. int tankstatus = 0;
  32. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  33. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  34. uint8_t zigbeestatu[20];
  35. uint8_t zigbeestatu1[5];
  36. uint8_t zigbeecon[7]="connect";
  37. uint8_t zigbeenotcon[11]="not connect";
  38. uint8_t zigbeechang[14]="change is down";
  39. uint16_t M1_AD_Value = 0;
  40. float M1_voltage_V =0;
  41. float M1_SonicESMUS07 =0;
  42. /* USER CODE BEGIN PV */
  43. typedef void (*pFunction)(void);
  44. /* USER CODE END PV */
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. void MX_ADC1_Init1(char pin);
  48. void sensor(void);
  49. /* USER CODE BEGIN PFP */
  50. /* USER CODE END PFP */
  51. /* Private user code ---------------------------------------------------------*/
  52. /* USER CODE BEGIN 0 */
  53. int _write(int fd, char *ch, int len)
  54. {
  55. HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
  56. HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
  57. return len;
  58. }
  59. void print(char* s, float c)
  60. { int w;
  61. int w_d;
  62. int w_f;
  63. if(c<0)
  64. {
  65. w = c*100;
  66. w_d = w/100;
  67. w_f = (-1*w)%100;
  68. }
  69. else
  70. {
  71. w = c*100;
  72. w_d = w/100;
  73. w_f = w%100;
  74. }
  75. printf("%s = %d.%d\n",s,w_d,w_f);
  76. }
  77. /* USER CODE END 0 */
  78. /**
  79. * @brief The application entry point.
  80. * @retval int
  81. */
  82. int main(void)
  83. {
  84. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  85. HAL_Init();
  86. SystemClock_Config();
  87. MX_GPIO_Init();
  88. MX_USART2_UART_Init();
  89. MX_UART4_Init();
  90. MX_USART1_UART_Init();
  91. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  92. HAL_Delay(1000);
  93. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  94. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  95. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  96. if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
  97. {
  98. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  99. }
  100. else if(zigbeestatu[18]== 0x05)
  101. {
  102. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  103. }
  104. while (1)
  105. {
  106. sensor();
  107. if(M1_SonicESMUS07<16)
  108. {
  109. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_RESET);
  110. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
  111. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
  112. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  113. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
  114. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  115. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  116. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
  117. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  118. HAL_Delay(5000);
  119. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_SET);
  120. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_SET);
  121. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_SET);
  122. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_SET);
  123. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_SET);
  124. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_SET);
  125. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_SET);
  126. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_SET);
  127. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_SET);
  128. }
  129. }
  130. }
  131. void SystemClock_Config(void)
  132. {
  133. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  134. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  135. __HAL_RCC_PWR_CLK_ENABLE();
  136. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  137. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  138. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  139. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  140. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  141. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  142. RCC_OscInitStruct.PLL.PLLM = 8;
  143. RCC_OscInitStruct.PLL.PLLN = 72;
  144. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  145. RCC_OscInitStruct.PLL.PLLQ = 3;
  146. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  147. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  148. {
  149. Error_Handler();
  150. }
  151. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  152. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  153. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  154. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  155. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  156. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  157. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  158. {
  159. Error_Handler();
  160. }
  161. }
  162. void user_pwm_setvalue(uint16_t value)
  163. {
  164. TIM_OC_InitTypeDef sConfigOC;
  165. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  166. sConfigOC.Pulse = value;
  167. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  168. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  169. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  170. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  171. }
  172. void user_pwm_setvalue2(uint16_t value)
  173. {
  174. TIM_OC_InitTypeDef sConfigOC;
  175. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  176. sConfigOC.Pulse = value;
  177. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  178. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  179. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  180. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  181. }
  182. void sensor(void)
  183. {
  184. MX_ADC1_Init1(ADC_CHANNEL_6);
  185. HAL_ADC_Start(&hadc1);
  186. HAL_ADC_PollForConversion(&hadc1, 50);
  187. if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
  188. {
  189. M1_AD_Value = HAL_ADC_GetValue(&hadc1);
  190. M1_voltage_V = M1_AD_Value*5.3f/4096;
  191. if (M1_voltage_V==0)
  192. {
  193. M1_SonicESMUS07 = 100;
  194. }
  195. else
  196. {
  197. M1_SonicESMUS07 = (M1_voltage_V*180)+100;
  198. }
  199. }
  200. print("M1_SonicESMUS07",M1_SonicESMUS07);
  201. HAL_Delay(10000);
  202. }
  203. void MX_ADC1_Init1(char pin)
  204. {
  205. ADC_ChannelConfTypeDef sConfig = {0};
  206. hadc1.Instance = ADC1;
  207. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  208. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  209. hadc1.Init.ScanConvMode = DISABLE;
  210. hadc1.Init.ContinuousConvMode = DISABLE;
  211. hadc1.Init.DiscontinuousConvMode = DISABLE;
  212. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  213. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  214. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  215. hadc1.Init.NbrOfConversion = 1;
  216. hadc1.Init.DMAContinuousRequests = DISABLE;
  217. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  218. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  219. {
  220. Error_Handler();
  221. }
  222. sConfig.Channel = pin;
  223. sConfig.Rank = 1;
  224. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  225. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  226. {
  227. Error_Handler();
  228. }
  229. }
  230. void MX_GPIO_Input1(unsigned long pin)
  231. {
  232. GPIO_InitTypeDef GPIO_InitStruct = {0};
  233. __HAL_RCC_GPIOE_CLK_ENABLE();
  234. GPIO_InitStruct.Pin = pin;
  235. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  236. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  237. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  238. }
  239. void Error_Handler(void)
  240. {
  241. }